package cn.wizzer.app.crown.modules.services.impl;

import cn.wizzer.app.crown.modules.models.CrownViewPoint;
import cn.wizzer.app.crown.modules.models.CrownViewPointOrder;
import cn.wizzer.app.crown.modules.models.vo.CrownViewPointOrderVo;
import cn.wizzer.app.crown.modules.services.ViewPointOrderService;
import cn.wizzer.app.crown.modules.services.ViewPointService;
import cn.wizzer.app.user.modules.models.enums.BizTypeEnum;
import cn.wizzer.app.user.modules.models.enums.ViewStatusEnum;
import cn.wizzer.app.user.modules.services.account.UserAccountRecordService;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import cn.wizzer.framework.base.service.CommonDBService;
import cn.wizzer.framework.page.Pagination;
import cn.wizzer.framework.util.Id;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;

import java.math.BigDecimal;

@IocBean(args = "refer:dao")
@Service(interfaceClass = ViewPointOrderService.class)
public class ViewPointOrderServiceImpl extends BaseServiceImpl<CrownViewPointOrder> implements ViewPointOrderService {

    public ViewPointOrderServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private ViewPointService viewPointService;

    @Reference
    @Inject
    private UserAccountRecordService accountRecordService;

    @Inject
    private CommonDBService dbService;

    @Override
    @Aop(TransAop.REPEATABLE_READ)
    public void add(String appid, String vpId) {
        CrownViewPoint vp = viewPointService.fetch(vpId);
        if (vp.getPrice() == null || vp.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
            Result.breakWith("无需购买");
        }
        if (this.count(Cnd.where("vpId", "=", vpId).and("userId", "=", appid)) != 0) {
            Result.breakWith("请勿重复购买");
        }
        CrownViewPointOrder vpo = new CrownViewPointOrder();
        vpo.setOpAt(1L);
        vpo.setUserId(appid);
        vpo.setCtAt(Times.getTS());
        vpo.setVpId(vpId);
        vpo.setNo(Id.createOrderNo());
        this.insert(vpo);
        //先付个款
        accountRecordService.add(appid, vp.getPrice(), BizTypeEnum.BUY_VIEW_POINT, vpo.getId());

    }

    /**
     * 观点订单列表查询
     *
     * @param pageNumber
     * @param pageSize
     * @param searchKeyword
     * @param searchDate
     * @param orderNo
     * @return
     */
    @Override
    public Pagination listPage(int pageNumber, int pageSize, String searchKeyword, String[] searchDate, String orderNo) {
        Sql sql = Sqls.create("SELECT cvpo.id, cvpo.no, cvpo.ctAt, " +
                "cvp.title, cvp.price, cvp.ctAt as viewReleaseAt, cvp.no as viewNo, " +
                "u.userName, u.nickName, u.phone " +
                "FROM crown_view_point_order cvpo ");
        Sql countSql = Sqls.create("SELECT COUNT(1) FROM crown_view_point_order cvpo ");
        String joinSql = "LEFT JOIN crown_view_point cvp ON cvpo.vpId = cvp.id " +
                "LEFT JOIN user u ON cvpo.userId = u.id " +
                "$condition ";
        sql.appendSourceSql(joinSql);
        countSql.appendSourceSql(joinSql);
        Cnd cnd = Cnd.where("cvpo.delFlag", "=", 0);
        if (Strings.isNotBlank(searchKeyword)) {
            searchKeyword = "%" + searchKeyword + "%";
            cnd.and(Cnd.exps("u.nickName", "LIKE", searchKeyword)
                    .or("u.phone", "LIKE", searchKeyword)
                    .or("u.userName", "LIKE", searchKeyword));
        }
        if (null != searchDate && searchDate.length != 0) {
            cnd.and("cvpo.ctAt", ">=", Times.d2TS(Times.D(searchDate[0])));
            cnd.and("cvpo.ctAt", "<=", Times.d2TS(Times.D(searchDate[1])));
        }
        if (Strings.isNotBlank(orderNo)) {
            orderNo = "%" + orderNo + "%";
            cnd.and("cvpo.no", "LIKE", orderNo);
        }
        sql.setCondition(cnd);
        sql.appendSourceSql("ORDER BY cvpo.ctAt DESC");
        countSql.setCondition(cnd);
        return dbService.listPage(CrownViewPointOrderVo.class, pageNumber, pageSize, sql.toString(), countSql.toString());
    }
}
